<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->

<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <!--      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="类型" v-model="listQuery.type">-->
      <!--      </el-input>-->
      <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
      <el-button v-if="sys_dict_add" class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary"
                 icon="edit">添加
      </el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit
              highlight-current-row style="width: 99%">
      <el-table-column align="center" label="编号">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="模块名">
        <template slot-scope="scope">
          <span>{{ scope.row.module }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="组件名" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.componentName }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="字段">
        <template slot-scope="scope">
          <span>{{ scope.row.fieldName }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="标签名" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="标签值">
        <template slot-scope="scope">
          <span>{{ scope.row.value }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" width="170">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button v-if="sys_dict_upd" size="small" type="success" @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-if="sys_dict_del" size="mini" type="danger" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="35%" >
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" >
        <el-form-item label="模块名" prop="componentName">
          <el-select v-model="form.module" placeholder="请选择模块">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="组件名" prop="componentName"  >
          <el-input   v-model="form.componentName" placeholder="组件名"></el-input>
        </el-form-item>
        <el-form-item label="字段名" prop="fieldName" >
          <el-input v-model="form.fieldName" placeholder="字段名"></el-input>
        </el-form-item>
        <el-form-item label="标签值" prop="value" >
          <el-input v-model="form.value" placeholder="标签值"></el-input>
        </el-form-item>
        <el-form-item label="标签名" prop="name" >
          <el-input v-model="form.name" placeholder="标签名"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort" >
          <el-select v-model="form.sort" >
            <el-option label="默认位置" :value="0"/>
            <el-option label="位置一" :value="1"/>
            <el-option label="位置二" :value="2"/>
            <el-option label="位置三" :value="3"/>
            <el-option label="位置四" :value="4"/>
            <el-option label="位置五" :value="5"/>
            <el-option label="位置六" :value="6"/>
            <el-option label="位置七" :value="7"/>
            <el-option label="位置八" :value="8"/>
            <el-option label="位置九" :value="9"/>
            <el-option label="位置十" :value="10"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
  .el-form-item .el-input {
    width: 300px;
  }
  .el-form-item .el-select {
    width: 300px;
  }

</style>

<script>
  import { fetchList, addObj, putObj, delObj } from '@/api/dict'
  import waves from '@/directive/waves/index.js' // 水波纹指令
  import { mapGetters } from 'vuex'

  export default {
    name: 'table_sys_dict',
    directives: {
      waves
    },
    data() {
      return {
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20
        },
        options: [
          {
            value: 'common',
            label: 'common'
          },
          {
            value: 'upms',
            label: 'upms'
          }, {
            value: 'store',
            label: 'store'
          }, {
            value: 'supplier',
            label: 'supplier'
          }, {
            value: 'user',
            label: 'user'
          }, {
            value: 'order',
            label: 'order'
          }],
        rules: {
          value: [
            {
              required: true,
              message: '数据值',
              trigger: 'blur'
            }
          ],
          label: [
            {
              required: true,
              message: '标签名',
              trigger: 'blur'
            }
          ],
          type: [
            {
              required: true,
              message: '类型',
              trigger: 'blur'
            }
          ],
          description: [
            {
              required: true,
              message: '描述',
              trigger: 'blur'
            }
          ],
          sort: [
            {
              required: true,
              message: '排序',
              trigger: 'blur'
            }
          ]
        },
        form: {
          module:"common",
          sort:0,
          id :undefined,
          module:undefined,
          value:undefined,
          name:undefined,
          fieldName:undefined,
          componentName:undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        sys_dict_add: false,
        sys_dict_upd: false,
        sys_dict_del: false,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0
      }
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          0: '有效',
          1: '无效'
        }
        return statusMap[status]
      }
    },
    created() {
      this.getList()
      this.sys_dict_add = this.permissions['sys_dict_add']
      this.sys_dict_upd = this.permissions['sys_dict_upd']
      this.sys_dict_del = this.permissions['sys_dict_del']
    },
    methods: {
      getList() {
        this.listLoading = true
        this.listQuery.orderByField = 'create_time'
        this.listQuery.isAsc = false
        fetchList(this.listQuery).then(response => {
          this.list = response.data.data.records
          this.total = response.data.data.total
          this.listLoading = false
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      handleDelete(row) {
        delObj(row).then(response => {
          this.dialogFormVisible = false
          this.getList()
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
        })
      },
      handleUpdate(row) {
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.form.id = row.id
        this.form.module = row.module
        this.form.value = row.value
        this.form.name = row.name
        this.form.fieldName = row.fieldName
        this.form.sort = row.sort
        this.form.componentName = row.componentName
      },
      handleCreate() {
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
      },
      create(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
          if (valid) {
            addObj(this.form).then(() => {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
          } else {
            return false
          }
        })
      },
      cancel(formName) {
        this.dialogFormVisible = false
        const set = this.$refs
        set[formName].resetFields()
      },
      update(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
          if (valid) {
            this.dialogFormVisible = false
            this.form.password = undefined
            putObj(this.form).then(() => {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
            })
          } else {
            return false
          }
        })
      }
    }
  }
</script>
